﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace SingletonUnit
{
    public delegate void DelegateSingletonTest();
    public sealed class DelegateTestHandler
    {
        private static readonly DelegateTestHandler _instance = new DelegateTestHandler();
        private SingletonType _DelegateTestType;
        static DelegateTestHandler() { }
        private DelegateTestHandler() { }
        public static DelegateTestHandler Instance
        {
            get { return _instance; }
        }
        private static IDictionary<SingletonType, string> DictionaryBaseSingletonTest
        {
            get
            {
                return new Dictionary<SingletonType, string>
                {
                    {SingletonType.Simple,"SingletonEntity,SingletonUnit.SimpleSingleton"},
                    {SingletonType.ThreadSafeNoLock,"SingletonEntity,SingletonUnit.ThreadSafeNoLockSingleton"},
                    {SingletonType.ThreadSafe,"SingletonEntity,SingletonUnit.ThreadSafeSingleton"},
                    {SingletonType.DoubleCheckedLocking,"SingletonEntity,SingletonUnit.DoubleCheckedLockingSingleton"},
                    {SingletonType.Lazy,"SingletonEntity,SingletonUnit.LazySingleton"},
                    {SingletonType.DotNetLazy,"SingletonEntity,SingletonUnit.DotNetLazySingleton"},
                };
            }
        }
        private void SingletonTest()
        {
            try
            {
                string[] cls = DictionaryBaseSingletonTest[_DelegateTestType].Split(',');
                ((BaseSingleton)(Assembly.Load(cls[0]).GetType(cls[1]).GetProperty("Instance").GetValue(null, null))).CreateTime.ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public DelegateSingletonTest GetDelegateSingletonTest(SingletonType delegateTestType)
        {
            _DelegateTestType = delegateTestType;
            return new DelegateSingletonTest(SingletonTest);
        }
    }
}
